<?php   // for adding project
session_start();
        include "include/top.php";
?>

<?php

$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'];
$project_name =  $_REQUEST['project_name'];
$project_description =  $_REQUEST['project_description'];

//$name = "test1";
//$password = "test1";

if (!isset($user_id ) )
{
echo "<h1> Needs to login</h1>";
echo "<br />";
//header("Location: /OSPMS/index.php");
} else 
{
// connect to mysql
echo "<h1> connecting to db</h1>";
$mysql = mysqli_connect("localhost", "ospms", "ospms");
if(!$mysql) {
echo "Cannot connect to database.";
exit;
}
//echo "<h1> connecting to db</h1>";
//echo "<h1> connected db</h1>";
// select the appropriate database
$selected = mysqli_select_db($mysql, "OSPMS");
if(!$selected) {
echo "Cannot select database.";
exit;
}

// query the database to see if there is a record which matches
$query = 'select count(*) from user_project  where project_name = '. $project_name ;
$result = mysqli_query($mysql, $query);
//var_dump($result);

$row = mysqli_fetch_row($result);

$count = $row[0];
if ($count = 0) {
echo "New Project";
}

$query = "INSERT INTO user_projects (user_id, project_name, project_description )". 
		" VALUES (" .$user_id. ", '" .$project_name. "','" .$project_description. "') ";
//$query = "INSERT INTO user_projects (user_id, project_name, project_description )". 
//		" VALUES ( 1" . ", '" .$project_name. "','" .$project_description. "') ";

echo "$query\n";
$result = mysqli_query($mysql, $query);
$row = mysqli_fetch_row($result);

if ( $result );
{
header("Location: /OSPMS/404.php");
}


//var_dump($row);
header("Location: /OSPMS/ShowProjects.php");

}
?>

<?php
//      include "include/bottom.php"
?>
